utils.js ➔ isLink   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 5
nc 2
nop 1
dl 0
loc 12
rs 9.3333
c 2
b 1
f 0
1
// ////////////////////////////////////////////////////////////////////////////////////
2
// Copyright © 2017 TangDongxin
3
//
4
// Permission is hereby granted, free of charge, to any person obtaining
5
// A copy of this software and associated documentation files (the "Software"),
6
// To deal in the Software without restriction, including without limitation
7
// The rights to use, copy, modify, merge, publish, distribute, sublicense,
8
// And/or sell copies of the Software, and to permit persons to whom the
9
// Software is furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// In all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
// ////////////////////////////////////////////////////////////////////////////////////
22
23
24
// ===========================================
25
// UTIL TOOLS
26
// ===========================================
27
28
29
function isBASE64 (str) {
0 ignored issues
show
Unused Code introduced by
The parameter str is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
30
31
  try {
32
33
    const res = new Buffer(a, 'base64').toString();
0 ignored issues
show
Bug introduced by
The variable a seems to be never declared. If this is a global, consider adding a /** global: a */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
34
    return res != undefined;
0 ignored issues
show
Best Practice introduced by
Comparing res to undefined using the != operator is not safe. Consider using !== instead.
Loading history...
35
36
  } catch (e) {
37
38
    return false;
39
40
  }
41
42
}
43
44
function isJSON (str) {
45
46
  if (typeof str === 'string') {
47
48
    try {
49
50
      if (str[0] == '"') {
51
52
        str = eval(str);
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
53
54
      }
55
56
    } catch (e) {
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
57
58
      // here we just ignore the result
59
      // dlog(e);
60
61
    }
62
63
64
    try {
65
66
      const obj = eval(`(${ str })`);
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
Unused Code introduced by
The constant obj seems to be never used. Consider removing it.
Loading history...
67
68
      if (str[0] == '{' || str[0] == '[') {
69
70
        return true;
71
72
      }
73
74
    } catch (e) {
75
76
      dlog(e);
77
78
    }
79
80
  }
81
  return false;
82
83
}
84
85
function isLink (str) {
86
87
  if (str.startsWith('http') || str.startsWith('mailto') || str.startsWith('\'http') || str.startsWith('"http')) {
88
89
    str = eval(str);
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
90
91
  }
92
93
  const regex = /^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?$/;
94
  return regex.test(str);
95
96
}
97
98
99
function showPopup (str, div) {
100
101
  div.innerHTML = str;
102
103
}
104